jQuery.extend({random: function(X) {return Math.floor(X * (Math.random() % 1));},randomBetween: function(MinV, MaxV) {return MinV + jQuery.random(MaxV - MinV + 1);}});
$(document).ready(function(){
	// Egg dialogs
	$.each($('.framework, .task, .cms, .startup'),function(k,v){
		$(v).hover(
			function(e){
				var el = $('.'+$(this).attr('class')+'-dialog');
				el.css('display','block');
			},
			function(e){
				var el = $('.'+$(this).attr('class')+'-dialog').css('display','none');
				el.css('display','none');
			}
		);
	});
	
	// The dyno
	var feet=$.randomBetween(1,10);
	window.setInterval(function(){
		window.setTimeout(function(){
			if(feet%2==0)
			{
				$('.feet').css({
					backgroundPosition:'top right',
					marginLeft: $.randomBetween(105,132),
					marginTop: $.randomBetween(170,220),
					display: 'none'
				}).fadeIn(900,function(){$(this).fadeOut(200)});
			}
			else
			{
				$('.feet').css({
					backgroundPosition:'0 0',
					marginLeft: $.randomBetween(10,40),
					marginTop: $.randomBetween(102,150),
					display: 'none'
				}).fadeIn(900,function(){$(this).fadeOut(200)});
			}
			feet = $.randomBetween(1,10);
		},$.randomBetween(500,3000));
	},1100);
	
	// Page shifter
	var shifting = false;
	var shift = function(el){
		var el = $(el);
		if(el && el.length >0)
		{
			var p = el.position();
			shifting = true;
			$('html,body').animate({scrollTop:p.top},1500,function(){shifting=false;});
		}
	};
	
	// Get the submenu top
	var submenu = $('.submenu');
	var submenuPosition = submenu.position();
	var content = $('#ferv_content');
	var framework,task,cms,startup;
	
	// Use buttons to navigate
	$('.submenu ul li').click(function(e){
		var cls = $.trim($(this).attr('class').replace(/current/,''));
		// Remove the dialog
		$('.'+cls+'-dialog').css('display','none');
		// Make this current
		$('.submenu ul li').removeClass('current');$(this).addClass('current');
		content.slideDown();
		$('.submenu .top').css('display','block');
		if(typeof framework == 'undefined')
		{
			framework = {top:$('.framework-content').position().top,height:$('.framework-content').outerHeight()};
		}
		if(typeof task == 'undefined')
		{
			task = {top:$('.task-content').position().top,height:$('.task-content').outerHeight()};
		}
		if(typeof cms == 'undefined')
		{
			cms = {top:$('.cms-content').position().top,height:$('.cms-content').outerHeight()};
		}
		if(typeof startup == 'undefined')
		{
			startup = {top:$('.startup-content').position().top,height:$('.startup-content').outerHeight()};
		}
		// Navigate to the given element
		shift('.'+cls+'-content');
	});
	
	$(window).scroll(function(e){
		if($(window).scrollTop() >= submenuPosition.top)
		{
			if(!submenu.hasClass('top-fixed'))
			{
				submenu.addClass('top-fixed');
				if(!shifting)
				{
					content.slideDown();
				}
			}
			if(!shifting)
			{
				if($(window).scrollTop()>=framework.top){$('.submenu ul li').removeClass('current');$('.submenu ul li.framework').addClass('current');}
				if($(window).scrollTop()>=task.top){$('.submenu ul li').removeClass('current');$('.submenu ul li.task').addClass('current');}
				if($(window).scrollTop()>=cms.top){$('.submenu ul li').removeClass('current');$('.submenu ul li.cms').addClass('current');}
				if($(window).scrollTop()>=startup.top){$('.submenu ul li').removeClass('current');$('.submenu ul li.startup').addClass('current');}
			}
		}
		else
		{
			submenu.removeClass('top-fixed');
			if(!shifting)
			{
				$('.submenu ul li').removeClass('current');
				content.slideUp();
				$('.submenu .top').css('display','none');
			}
		}
	});
	
	// Go to top
	$('.submenu .top').click(function(){
		$('html,body').animate({scrollTop:0},1500);
	});
	
	
});